package com.security.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.csrf.DefaultCsrfToken;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.security.controller.SecurityController;

/**
 * 自定义登录验证失败调用
 * @author chengr
 *
 */
@Service("failureHandler")
public class LoginAuthenticationFailureHandler implements
		AuthenticationFailureHandler {
	//默认错误页面
	private String defaultFailureUrl="/login.html";  
    
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {
		System.out.println("!========go==========onAuthenticationFailure");
		SecurityController.formatHeader(response);
		Map<String,Object> res = new HashMap<String,Object>();
		res.put("rstUrl", defaultFailureUrl);
		JSONObject json = new JSONObject(res);
		response.getWriter().print(json.toJSONString());
	}
}
